<?php


namespace XinYaTable\Forms\Repositories;


use XinYaTable\Forms\Models\EloquentForm;

class FormRepository extends BaseRepository {
    private $_eloquentForm;
    
    public function __construct(EloquentForm $eloquentForm) {
        parent::__construct();
        $this->_eloquentForm = $eloquentForm;
    }
    
    /**
     * @param int $id
     *
     * @return mixed
     */
    public function getForm(int $id) {
        return $this->_eloquentForm
            ->newQuery()
            ->select(['id', 'title', 'table_name', 'icon'])
            ->usingForm($id)
            ->with('fields')
            ->first();
    }
    
    /**
     * @param int $id
     *
     * @return mixed
     */
    public function getFormByTableName($tableName, $isOnlyId = false) {
        $itemEl = $this->_eloquentForm
            ->newQuery()
            ->select(['id', 'title', 'table_name', 'icon'])
            ->usingForm($tableName, \XinYaTable\Forms\Providers\FormServiceProvider::$formTableIdentityColumn)
            ->with('fields')
            ->first();
    
        return $isOnlyId ? ($itemEl ? $itemEl->id : 0) : $itemEl;
    }
    
    /**
     * @param int $id
     *
     * @return mixed
     */
    public function getFormById(int $id){
        return $this->_eloquentForm->find(1);
    }
}